/*
 * Copyright (c) 2007, TopCoder, Inc. All rights reserved.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using TopCoder.Util.Calendar.Entry;

namespace TopCoder.WinForms.Controls.CalendarView.Views
{
    /// <summary>
    /// Extension of CalendarEntryPopupPanel that renders the type and entry description of an CalendarEntry
    /// in the Weekly View in a popup if the panel display is truncated.
    /// </summary>
    /// <remarks>Thread Safety: This class is immutable and thread-safe.</remarks>
    /// <author>Ghostar</author>
    /// <author>yext</author>
    /// <author>argolite</author>
    /// <author>alex.dolin</author>
    /// <version>1.1</version>
    /// <copyright>Copyright (c) 2007, TopCoder, Inc. All rights reserved.</copyright>
    public class DescriptionPopupPanel : CalendarEntryPopupPanel
    {
        /// <summary>
        /// Creates a new instance of this class.
        /// </summary>
        /// <param name="entry">The entry for this panel.</param>
        /// <param name="truncated">The flag whether the data is truncated.</param>
        /// <param name="calendarView">The parent calendar view.</param>
        /// <param name="destinationViewName">The destination view's name
        /// for the calendar view control.</param>
        /// <param name="dateOfInterest">The date around which to wrap the destination view.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="entry"/>
        /// or <paramref name="calendarView"/> is null.</exception>
        public DescriptionPopupPanel(CalendarEntry entry, bool truncated, AbstractCalendarView calendarView,
                                     string destinationViewName, DateTime dateOfInterest)
            : base(entry, truncated, calendarView, destinationViewName, dateOfInterest)
        {
        }

        /// <summary>
        /// Processes the display panel paint event.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">The event, containing the graphic.</param>
        protected override void OnPopupPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            // Draw the text
            g.DrawString(Entry.FullName, WeeklyCalendarView.itemFont,
                         WeeklyCalendarView.itemBrush,
                         WeeklyCalendarView.PADDING_LEFT, WeeklyCalendarView.PADDING_TOP);
        }

        /// <summary>
        /// <para>Displays the popup on the screen.</para>
        /// <para>Implementations must check whether the popup is required,
        /// determine size of the popup and then show it.</para>
        /// <para>Method <see cref="PopupPanel.ShowPopup"/> should be used to display
        /// the popup in default position. Implementations should create and use
        /// <see cref="Graphics"/> of the <paramref name="display"/> panel to measure the size.</para>
        /// </summary>
        /// <param name="display">The panel that will hold all drawn data.</param>
        /// <param name="popup">The popup control that holds the panel data.</param>
        protected override void DrawPanel(Panel display, ToolStripDropDown popup)
        {
            if (Truncated)
            {
                using (Graphics g = display.CreateGraphics())
                {
                    SizeF size = g.MeasureString(Entry.Type.Description
                                                 + ": " + Entry.Description, WeeklyCalendarView.itemFont);
                    float measuredWidth =
                        size.Width + WeeklyCalendarView.PADDING_LEFT + WeeklyCalendarView.PADDING_RIGHT;
                    float measuredHeight =
                        size.Height + WeeklyCalendarView.PADDING_TOP + WeeklyCalendarView.PADDING_BOTTOM;

                    // Adjust the size of the popup panel
                    ShowPopup((int)Math.Ceiling(measuredWidth), (int)Math.Ceiling(measuredHeight));
                }
            }
        }
    }
}